import BreadcrumbDept from '@/components/BreadcrumbDept';
import PermissionControl from '@/components/PermissionControl/index';
import { deletePatientInfo, getPatientList } from "@/services/patient/patientApi";
import { showMsg } from "@/utils/utils";
import { PlusOutlined } from "@ant-design/icons";
import { PageContainer } from "@ant-design/pro-layout";
import ProTable, { ActionType, ProColumns } from "@ant-design/pro-table";
import { Button, Popconfirm } from "antd";
import { useRef, useState } from 'react';
import DetailModal from './DetailModal';
import EditForm from "./EditModal";
import './style.less';

const Information = () => {

  const actionRef = useRef<ActionType>();
  const [editModalOpen, setEditModalOpen] = useState<boolean>(false);
  const [detailModalOpen, setDetailModalOpen] = useState<boolean>(false);
  const [selectedRowData, setSelectedRowData] = useState<PATIENT.PatientInfo>();

  const columns: ProColumns<PATIENT.PatientInfo>[] = [
    {
      title: '身份证/住院号',
      dataIndex: 'identity_no',
      colSize: 3
    },
    {
      title: '患者编号',
      dataIndex: 'patient_id',
      search: false
    },
    {
      title: '患者姓名',
      dataIndex: 'patient_name',
      colSize: 2.5
    },
    {
      title: '手机号',
      dataIndex: 'mobile',
      colSize: 2.5
    },
    {
      title: '积分',
      dataIndex: 'available_integral',
      search: false
    },
    {
      title: '等级',
      dataIndex: 'rank',
      valueType: 'digit',
      colSize: 2.5
    },
    {
      title: '操作',
      valueType: 'option',
      align: 'center',
      width: '15%',
      render: (_, record, idx, action) => [
        <PermissionControl key="detail" action_id="400401019">
          <a onClick={() => { setSelectedRowData(record); setDetailModalOpen(true); }}>详情</a>
        </PermissionControl>,
        <PermissionControl key="edit" action_id="900900003">
          <a onClick={() => { setSelectedRowData(record); setEditModalOpen(true); }}>编辑</a>
        </PermissionControl>,
        <PermissionControl key="del" action_id="900900004">
          <Popconfirm title="确认删除？" onConfirm={async () => {
            if (!record.record_id)
              return;
            try {
              await deletePatientInfo(record.record_id);
              showMsg('删除成功', 'success');
              action?.reload();
            } catch (error: any) {
              showMsg(error?.data?.errmsg);
            }
          }}>
            <a>删除</a>
          </Popconfirm>
        </PermissionControl>
      ]
    }
  ];

  return (
    <PageContainer extra={<BreadcrumbDept />} breadcrumb={{}}>
      <ProTable<PATIENT.PatientInfo>
        rowKey="record_id"
        actionRef={actionRef}
        className='mytable'
        bordered
        search={{
          span: 2,
          labelWidth: 'auto',
          collapsed: false,
          collapseRender: false,
          style: { padding: '0 4px 0 18px' },
          optionRender: (_, __, [, queryBtn]) => [queryBtn]
        }}
        request={async (params) => {
          try {
            const res = await getPatientList({ ...params });
            return { success: true, data: res.record_list, total: res.sum_record_num };
          } catch (reason: any) {
            showMsg(reason?.data?.errmsg);
          }
          return { success: false, data: [] };
        }}
        options={false}
        columns={columns}
        toolBarRender={() => [
          <PermissionControl key="add" action_id="900900001">
            <Button type="primary" icon={<PlusOutlined />} onClick={() => setEditModalOpen(true)}>新增</Button>
          </PermissionControl>
        ]}
        pagination={{ showQuickJumper: true }}
      />

      <EditForm open={editModalOpen} rowData={selectedRowData} onOpenChange={open => {
        if (!open) {
          actionRef.current?.reload();
          setSelectedRowData(undefined);
        }
        setEditModalOpen(open);
      }} />

      <DetailModal open={detailModalOpen} onOpenChange={setDetailModalOpen} rowData={selectedRowData} />
    </PageContainer>
  );
};

export default Information;
